﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace llibreria_alsas
{
    class ConversorTemas : BDConnexio
    {
        public static void afegirTemas(Temas tema)
        {
            int idResultat = 0;
            SqlConnection con = obtenirConnexio();

            con.Open();

            SqlCommand selectId = new SqlCommand("Select max(id) from Temas", con);
            Object val = selectId.ExecuteScalar();
            if (val != DBNull.Value)
            {
                idResultat = Convert.ToInt32(selectId.ExecuteScalar().ToString());
                idResultat++;
            }
            else
                idResultat = 1;


            SqlCommand insert = new SqlCommand("INSERT INTO Temas VALUES(" + idResultat + ",'" + tema.Tema + "')", con);
            insert.ExecuteNonQuery();

            con.Close();
        }

        public static DataTable seleccionarTemas()
        {
            SqlConnection con = obtenirConnexio();
            DataTable temas = new DataTable();
            SqlDataAdapter Dadapter;

            con.Open();

            Dadapter = new SqlDataAdapter("Select id, tema from temas", con);
            Dadapter.Fill(temas);

            con.Close();

            return temas;
        }

        public static void eliminarTema(int id)
        {
            SqlConnection con = obtenirConnexio();
            con.Open();
            SqlCommand deleteId = new SqlCommand("Delete from Temas where id = " + id, con);
            deleteId.ExecuteNonQuery(); 
        }
    }
}
